﻿var flashObj = null;
var P_albumsId = null;
var P_albumsURL = null;
$(function(){
	initTab();
	initEvent();
})

function initFlex(){
	try {
		flashObj = swfobject.getObjectById("AlbumsUpload");
		flashObj.MXMLInitParams({
			url:"*.AJAX",
			jsFunction:"upload",
			filter:"*.jpg;*.png;*.gif",
			method:"post",
			miniMargin:10,
			miniSize:110,
			responseFunction:"response",
			errFunction:"errFunction",
			maxNums:1
		});
		flashObj.MXMLSetParam("SERVICENAME","albums.uploadAlbumsImg");
		flashObj.MXMLSetParam("nId",P_albumsId);
		if(P_albumsURL != null) {
			flashObj.MXMLInitImage("../../images/albums/"+P_albumsURL);
		}
	}catch(e){
		if(!flashObj.MXMLInitParams) {
			setTimeout("initFlex()",100);
		}
	}
}

function response(response){
	alert(response);
}

function upload(){
	flashObj.MXMLInitImage("../../images/albums/"+$("#albumsId").val()+".bin?key="+new Date().getUTCMilliseconds());
	$("#albumsFM_"+$("#albumsId").val()).attr("src","../../images/albums/"+$("#albumsId").val()+".bin?key="+new Date().getUTCMilliseconds());
	alert("封面照片上传成功!");
	
}

function errFunction(err){
	alert(err);
}

function initTab(){
	$("li a", $(".nav")).click(function(){
		var $this = $(this);
		if($this.hasClass("active")){
			return;
		}
		
		$this.parent().siblings().find("a").removeClass("active");
		$this.addClass("active");
		if($(this).attr("id") == "albums_new") {
			$("#divAlbums").css("display","");
			$("#divImage").css("display","none");
			$("#divUpdAlbums").css("display","none");
		}else if($(this).attr("id") == "albums_upd") {
			$("#albums_upd").css("display","none");
			flashObj = null;
			$("#divAlbums").css("display","none");
			$("#divUpdAlbums").css("display","");
			$("#divImage").css("display","none");
		}else{
			$("#albums_upd").css("display","none");
			flashObj = null;
			$("#divAlbums").css("display","none");
			$("#divUpdAlbums").css("display","none");
			$("#divImage").css("display","");
		}
	}).focus(function(){
		$(this).blur();
		
	});
	
	$("#albums_manager").triggerHandler('click');
}

function initEvent() {
	$("#btnSave").click(function (){
		if($.trim($("#cXcmc").val()) == "") {
			alert("请输入相册名称");
			$("#cXcmc").focus();
			return;
		}
		var gcSave = new AjaxProxy();
		gcSave.addParm("cXcmc",$("#cXcmc").val());
		gcSave.addParm("cDomain",$("#cXcms").val());
		gcSave.invoke("albums.saveUserAlbums",false);
		var flag = gcSave.getString("PN_RET");
		if(flag != 0){
			alert(gcSave.getString("PC_MSG"));
		}else{
			alert("相册保存成功!");	
			location.reload();
		}
	});
	
	$(".albums","#divImage").each(function(){
		var $this = $(this);
		$this.hover(function(){
			$("#showOrHide",$this).eq(0).css({display:''});
			$this.css({cursor:'pointer'});
		},function(){
			$("#showOrHide",$this).eq(0).css({display:'none'});
		});
		
		$("img",$this).eq(0).click(function(){
			parent.$("#content").attr("src","/filter/image/albumsImage?id="+$this.attr("v0")+"&name="+$this.attr("v1"));
		});
		
		var $imgs = $("#showOrHide",$this).eq(0);
		$("img",$imgs).each(function(){
			$img = $(this);
			$img.bind('click',function(){
				if($(this).attr("type") == "1") {//修改
					$("#albums_upd").css("display","");
					$("#albums_upd").triggerHandler('click');
					$("#albumsId").val($(this).attr("albumsId"));
					if(flashObj == null){
						P_albumsId = $(this).attr("albumsId");
						P_albumsURL = $(this).attr("albumsUrl")+"?key="+new Date().getUTCMilliseconds();
						initFlex();
					}else{
						flashObj.MXMLSetParamNull();
						flashObj.MXMLSetParam("SERVICENAME","albums.uploadAlbumsImg");
						flashObj.MXMLSetParam("nId",$(this).attr("albumsId"));
						if($(this).attr("albumsUrl") != null) {
							flashObj.MXMLInitImage("../../images/albums/"+$(this).attr("albumsUrl")+"?key="+new Date().getUTCMilliseconds());
						}
					}
					$("#cXcmc1").val($(this).attr("albumsName"));
					$("#cXcms1").val($(this).attr("albumsSm")=="null"?"":$(this).attr("albumsSm"));
				} else if($(this).attr("type") == "2") {//删除
					if(!confirm("确定要删除相册【"+$(this).attr("albumsName")+"】吗？"))return;
					var del = new AjaxProxy();
					del.addParm("nId",$(this).attr("albumsId"));
					del.invoke("albums.deleteUserAlbums",false);
					var flag = del.getString("PN_RET");
					if(flag != 0) {
						alert(del.getString("PC_MSG"));
					}else{
						alert("删除成功");
						location.reload();
					}
				} else {//上传
					
				}
			});
		});
	});
	
	$("#btnUpd").click(function(){
		if($.trim($("#cXcmc1").val() == "")){
			alert("相册名称不能为空");
			$("#cXcmc1").focus();
			return;
		}
		if(!confirm("确定要修改相册吗？"))return;
		var upd = new AjaxProxy();
		upd.addParm("nId",$("#albumsId").val());
		upd.addParm("cXcmc",$("#cXcmc1").val());
		upd.addParm("cDomain",$("#cXcms1").val());
		upd.invoke("albums.updUserAlbums",false);
		var flag = upd.getString("PN_RET");
		if(flag != 0){
			alert(upd.getString("PC_MSG"));	
		}else{
			alert("修改相册成功!");
			location.reload();
		}
	});
	
	$("#btnClose").click(function(){
		$("#albums_upd").css("display","none");
		$("#albums_manager").triggerHandler('click');
		flashObj = null;
	});
}